package com.by.course.controller.teacher;

import com.by.course.config.web.jwt.PassToken;
import com.by.course.entity.base.BaseResult;
import com.by.course.entity.bo.BaseUser;
import com.by.course.entity.vo.appointment.req.UpdateBaseAppointmentVO;
import com.by.course.entity.vo.lessonreview.req.AddBaseLessonReviewVO;
import com.by.course.entity.vo.lessonreview.req.DeleteBaseLessonReviewVO;
import com.by.course.entity.vo.lessonreview.req.GetBaseLessonReviewVO;
import com.by.course.entity.vo.lessonreview.req.UpdateBaseLessonReviewVO;
import com.by.course.entity.vo.lessonreview.resp.BaseLessonReviewResultVO;
import com.by.course.entity.vo.schedule.req.AddBaseScheduleVO;
import com.by.course.entity.vo.schedule.req.GetBaseScheduleVO;
import com.by.course.entity.vo.schedule.resp.BaseScheduleDetailResultVO;
import com.by.course.entity.vo.schedule.resp.BaseScheduleResultVO;
import com.by.course.entity.vo.schedule.resp.BaseScheduledStudentResultVO;
import com.by.course.entity.vo.user.req.GetBaseUserVO;
import com.by.course.execption.BusinessException;
import com.by.course.manage.BaseCourseManager;
import com.by.course.serivce.BaseAppointmentService;
import com.by.course.serivce.BaseLessonReviewService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/teacher")
@Api(tags = "老师接口")
public class TeacherController {

    @Resource
    private BaseCourseManager baseCourseManager;

    @Resource
    private BaseLessonReviewService baseLessonReviewService;

    @Resource
    private BaseAppointmentService baseAppointmentService;

    @Value("${upload.evaluateImg}")
    private String evaluateImg;

    @Value("${upload.evaluateImgUrl}")
    private String evaluateImgUrl;

    @ApiOperation("排课管理--老师排课")
    @PostMapping("/createSchedule")
    public BaseResult<String> createSchedule(@RequestBody AddBaseScheduleVO vo) {
        return baseCourseManager.createSchedule(vo);
    }

//    @ApiOperation("排课管理--老师更新排课")
//    @PostMapping("/updateSchedule")
//    public BaseResult<String> updateSchedule(@RequestBody UpdateBaseScheduleVO vo) {
//        return baseCourseManager.updateSchedule(vo);
//    }

    @ApiOperation("排课管理--查看我的排课")
    @PostMapping("/getScheduleByUserId")
    public BaseResult<BaseScheduleResultVO> getScheduleByUserId(@RequestBody GetBaseScheduleVO vo) {
        return baseCourseManager.getScheduleByUserId(vo);
    }

//    @ApiOperation("排课管理--查看排课")
//    @PostMapping("/getSchedule")
//    public BaseResult<PageResult<BaseScheduleResultVO>> getSchedule(@RequestBody GetBaseScheduleVO vo) {
//        return baseCourseManager.getSchedule(vo);
//    }

//    @ApiOperation("排课管理--关闭我的某节课")
//    @PostMapping("/closeSchedule")
//    public BaseResult<String> closeSchedule(@RequestBody DeleteBaseScheduleVO vo) {
//        return baseCourseManager.closeSchedule(vo);
//    }

    @ApiOperation("评价管理--创建评价")
    @PostMapping("/createEvaluate")
    public BaseResult<String> createEvaluate(@RequestBody AddBaseLessonReviewVO vo) {
        return baseLessonReviewService.createEvaluate(vo);
    }

    @ApiOperation("评价管理--修改评价")
    @PostMapping("/updateEvaluate")
    public BaseResult<String> updateEvaluate(@RequestBody UpdateBaseLessonReviewVO vo) {
        return baseLessonReviewService.updateEvaluate(vo);
    }

    @ApiOperation("评价管理--删除评价")
    @PostMapping("/deleteEvaluate")
    public BaseResult<String> deleteEvaluate(@RequestBody DeleteBaseLessonReviewVO vo) {
        return baseLessonReviewService.deleteEvaluate(vo);
    }

    @ApiOperation("评价管理--查看评价")
    @PostMapping("/getEvaluateByTeacher")
    public BaseResult<BaseLessonReviewResultVO> getEvaluateByTeacher(@RequestBody GetBaseLessonReviewVO vo) {
        return baseLessonReviewService.getEvaluateByTeacher(vo);
    }

    @ApiOperation("评价管理--查看所有评价")
    @PostMapping("/listEvaluateByTeacher")
    public BaseResult<List<BaseLessonReviewResultVO>> listEvaluateByTeacher(@RequestBody GetBaseLessonReviewVO vo) {
        return baseLessonReviewService.listEvaluateByTeacher(vo);
    }
//
//    @ApiOperation("评价管理--老师查看评价")
//    @PostMapping("/queryEvaluateByTeacher")
//    public BaseResult<PageResult<BaseLessonReviewResultVO>> queryEvaluateByTeacher(@RequestBody GetBaseLessonReviewVO vo) {
//        return baseLessonReviewService.queryLessonReview(vo);
//    }

    @ApiOperation("签到管理--老师查看一个月内的列表")
    @PostMapping("/listScheduler")
    public BaseResult<List<BaseScheduleDetailResultVO>> listSchedule(@RequestBody GetBaseScheduleVO vo) {
        return baseCourseManager.listSchedule(vo);
    }

    @ApiOperation("签到管理--老师查看某个排课的学生列表")
    @PostMapping("/listStudentScheduler")
    public BaseResult<List<BaseScheduledStudentResultVO>> listStudentScheduler(@RequestBody GetBaseScheduleVO vo) {
        return baseCourseManager.listStudentScheduler(vo);
    }

    @ApiOperation("签到管理--学生上课签到（上课和未来上课）")
    @PostMapping("/updateStudentAppointment")
    public BaseResult<String> updateStudentAppointment(@RequestBody UpdateBaseAppointmentVO vo) {
        List<UpdateBaseAppointmentVO> voList = new ArrayList<>();
        voList.add(vo);
        return baseAppointmentService.updateAppointment(voList);
    }

    @PassToken
    @ApiOperation("文件轮播图接口")
    @PostMapping("/uploadImage")
    public BaseResult<String> uploadUser(@RequestParam("file") MultipartFile file) {
        String result = "";
        if (file == null || file.isEmpty()) {
            log.warn("uploadImage no file");
        } else {
            try {
                String originalFilename = file.getOriginalFilename();
                // 文件名加上时间戳，防止重复
                assert originalFilename != null;
                String[] split = originalFilename.split("\\.");
                result = split[0] + "_" + System.currentTimeMillis() + "." + split[1];
                Path filePath = Paths.get(evaluateImg + result);
                // 将文件保存到指定路径
                long copy = Files.copy(file.getInputStream(), filePath, StandardCopyOption.REPLACE_EXISTING);
                log.info("uploadImage succ:{} name:{}", copy, originalFilename);
            } catch (IOException e) {
                log.error("uploadImage failed", e);
                throw new BusinessException("文件上传失败");
            }
        }
        return new BaseResult.Builder<String>().setData(evaluateImgUrl + result).build();
    }

    @ApiOperation("代预约管理--查看学生列表")
    @PostMapping("/listStudent")
    public BaseResult<List<BaseUser>> listStudent(@RequestBody GetBaseUserVO vo) {
        return baseCourseManager.listStudent(vo);
    }
}
